package org.tnc.budget.model;

import static javax.persistence.GenerationType.IDENTITY;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Proxy;
import org.tnc.model.User;

/**
 * EventTable entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "event_table", catalog = "tnc")
public class EventTable implements java.io.Serializable {

	// Fields

	private Integer id;
	private BudgetCenter budgetCenter;
	private String startDate;
	private String startTime;
	private String endDate;
	private String endTime;
	private String eventName;
	private String eventAdd;
	private String eventText;
	private String eventId;
	private Boolean isValid;
	private Boolean isReviewed;
	private Boolean isPassed;
	private String reviewRemarks;
	private User user;
	private Date eventDate;
	@OneToMany(mappedBy="eventTable", cascade=CascadeType.ALL)
	private Set<AttachmentTable> attachmentTables = new HashSet<AttachmentTable>(
			0);

	// Constructors

	/** default constructor */
	public EventTable() {
	}

	/** minimal constructor */
	public EventTable(User user, Timestamp eventDate, Boolean isValid,
			String eventId) {
		this.user = user;
		this.eventDate = eventDate;
		this.isValid = isValid;
		this.eventId = eventId;
	}
	
	@Transient
	public double getDuration() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date start = df.parse(this.getStartDate() + " " + this.getStartTime());
			Date end = df.parse(this.getEndDate() + " " + this.getEndTime());
			
			return Math.abs(end.getTime() - start.getTime()) / 1000.0 / 60 / 60;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/** full constructor */
	public EventTable(BudgetCenter budgetCenter, User user,
			String startDate, String startTime, String endDate, String endTime,
			String eventName, String eventAdd, String eventText,
			Timestamp eventDate, Boolean isValid, String eventId,
			Set<AttachmentTable> attachmentTables) {
		this.budgetCenter = budgetCenter;
		this.user = user;
		this.startDate = startDate;
		this.startTime = startTime;
		this.endDate = endDate;
		this.endTime = endTime;
		this.eventName = eventName;
		this.eventAdd = eventAdd;
		this.eventText = eventText;
		this.eventDate = eventDate;
		this.isValid = isValid;
		this.eventId = eventId;
		this.attachmentTables = attachmentTables;
	}

	// Property accessors
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "fk_bc_id")
	public BudgetCenter getBudgetCenter() {
		return this.budgetCenter;
	}

	public void setBudgetCenter(BudgetCenter budgetCenter) {
		this.budgetCenter = budgetCenter;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "event_people", nullable = false)
	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Column(name = "start_date", length = 100)
	public String getStartDate() {
		return this.startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	@Column(name = "start_time", length = 100)
	public String getStartTime() {
		return this.startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	@Column(name = "end_date", length = 100)
	public String getEndDate() {
		return this.endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	@Column(name = "end_time", length = 100)
	public String getEndTime() {
		return this.endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	@Column(name = "event_name")
	public String getEventName() {
		return this.eventName;
	}

	public void setEventName(String eventName) {
		this.eventName = eventName;
	}

	@Column(name = "event_add")
	public String getEventAdd() {
		return this.eventAdd;
	}

	public void setEventAdd(String eventAdd) {
		this.eventAdd = eventAdd;
	}

	@Column(name = "event_text", length = 65535)
	public String getEventText() {
		return this.eventText;
	}

	public void setEventText(String eventText) {
		this.eventText = eventText;
	}
	
	@Column(name = "is_valid", nullable = false)
	public Boolean getIsValid() {
		return this.isValid;
	}

	public void setIsValid(Boolean isValid) {
		this.isValid = isValid;
	}
	
	@Column(name = "event_id", nullable = false, length = 200)
	public String getEventId() {
		return this.eventId;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}

	@Column(name = "event_date", nullable = false, length = 0)
	public Date getEventDate() {
		return this.eventDate;
	}

	public void setEventDate(Date eventDate) {
		this.eventDate = eventDate;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "eventTable" )
	public Set<AttachmentTable> getAttachmentTables() {
		return this.attachmentTables;
	}

	public void setAttachmentTables(Set<AttachmentTable> attachmentTables) {
		this.attachmentTables = attachmentTables;
	}

	@Column(name="bc_mgr_reviewed", nullable=true)
	public Boolean getIsReviewed() {
		return isReviewed;
	}

	public void setIsReviewed(Boolean isReviewed) {
		this.isReviewed = isReviewed;
	}

	@Column(name="bc_mgr_remarks")
	public String getReviewRemarks() {
		return reviewRemarks;
	}

	public void setReviewRemarks(String reviewRemarks) {
		this.reviewRemarks = reviewRemarks;
	}

	@Column(name="bc_mgr_review_passed", nullable=true)
	public Boolean getIsPassed() {
		return isPassed;
	}

	public void setIsPassed(Boolean isPassed) {
		this.isPassed = isPassed;
	}

}